package com.m2h.xiaoke.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;

import com.blankj.utilcode.util.LogUtils;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.WakeuperListener;
import com.iflytek.cloud.WakeuperResult;
import com.m2h.xiaoke.activity.HomeActivity;
import com.m2h.xiaoke.api.XunfeiAwaken;
import com.m2h.xiaoke.api.XunfeiVoice;

/**
 * Created by lijh on 2018/6/2.
 *
 * @author lijh
 */

public class VoiceService extends Service {

	public static boolean isRunning = false;

	private Context mContext = null;
	@Override
	public void onCreate() {
		super.onCreate();
		isRunning = true;
		mContext = this;
		XunfeiVoice.getInstance().initVoice(this);
	}
	@Override
	public Binder onBind(Intent intent) {
		return null;
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		XunfeiAwaken.getInstance().startAwaken(mWakeUWakeuperListener);
		return START_STICKY;
	}
	@Override
	public void onDestroy() {
		XunfeiAwaken.getInstance().stopAwaken();
		XunfeiVoice.getInstance().uninitVoice();
		isRunning = false;
		mContext = null;
		super.onDestroy();
	}

	private WakeuperListener mWakeUWakeuperListener = new WakeuperListener() {
		@Override
		public void onBeginOfSpeech() {

		}

		@Override
		public void onResult(WakeuperResult wakeuperResult) {
			LogUtils.d("onAwakenUp:result"+wakeuperResult.getResultString());
			HomeActivity.startActivity(mContext);
		}

		@Override
		public void onError(SpeechError speechError) {

		}

		@Override
		public void onEvent(int i, int i1, int i2, Bundle bundle) {

		}

		@Override
		public void onVolumeChanged(int i) {

		}
	};
}
